//
//  NewConstructAlbumViewController.m
//  jingdongfang
//
//  Created by MTiMac02 on 2017/10/31.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "NewConstructAlbumViewController.h"
#import "IGAddAtlasApi.h"
#import "MyAltasViewController.h"

@interface NewConstructAlbumViewController ()
@property (strong, nonatomic) IBOutlet UITextField *NewTextField;
@property(nonatomic,strong)IGAddAtlasApi *addAtlasApi;
@end

@implementation NewConstructAlbumViewController

#pragma mark -生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor c2Color];
//    self.navigationItem.title=@"新建图册";
    self.navigationItem.title=NSLocalizedString(@"New Album", nil);
//    [self ig_setupRightButtonItemWithTitle:@"确定" image:nil highLightedImage:nil];
    [self ig_setupRightButtonItemWithTitle:NSLocalizedString(@"Confirm", nil) image:nil highLightedImage:nil];
    [self setUI];
    // Do any additional setup after loading the view from its nib.
}
-(void)setUI{
//    _NewTextField.placeholder=@"新建图册";
    _NewTextField.placeholder=NSLocalizedString(@"New Album", nil);
    _NewTextField.leftViewMode = UITextFieldViewModeAlways;
    _NewTextField.rightViewMode = UITextFieldViewModeUnlessEditing;
    _NewTextField.keyboardType=UIKeyboardTypeDefault;
    _NewTextField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 8, 0)];
    _NewTextField.font = [UIFont font16];
   _NewTextField.textColor=[UIColor c4Color];
    _NewTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [_NewTextField addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
    
}
//导航栏右侧按钮点击事件
- (void)ig_rightButtonPressed:(id)sender {
    if (_NewTextField.text.length==0) {
//        _NewTextField.text=@"新建图册";
//        [self presentMessageTips:@"请输入图册名"];
        [self presentMessageTips:NSLocalizedString(@"Please Input Album Name", nil)];
        return;
    }
    BOOL ishas = [NSString stringContainsEmoji:_NewTextField.text];
    NSLog(@" %@ 表情", ishas?@"有":@"没有");
    if (ishas)
    {
//        [self presentMessageTips:@"不支持表情符号"];
        [self presentMessageTips:NSLocalizedString(@"Emoji Unavailable", nil)];
        return;
    }
    self.addAtlasApi=[[IGAddAtlasApi alloc]initWithUid:[IGUser getUid] title:_NewTextField.text];
    [self.addAtlasApi startWithDecodeClass:[IGAddAtlasResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error)
     {
         if (responseObject) {
//             [self presentMessageTips:@"新建成功"];
             [self presentMessageTips:NSLocalizedString(@"Create Album Successfully", nil)];

             [UIViewController ig_popViewController:[MyAltasViewController class]];
         }else{
//             [self presentMessageTips:@"新建失败"];
             [self presentMessageTips:NSLocalizedString(@"Create New Failed", nil)];

         }
     }];
}
//编辑图册名称
- (void)textChange:(UITextField *)textView {
    NSString *toBeString=textView.text;
    UITextRange *selectedRang=[textView markedTextRange];
    UITextPosition *position=[textView positionFromPosition:selectedRang.start offset:0];
    if (!position) {
        NSStringEncoding enc=CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000 );
        NSData *da=[toBeString dataUsingEncoding:enc];
        NSInteger dataLength=[da length];
        if(dataLength>20){
            int lll=(int)[toBeString length];
            for (int z=0; z<lll; z++) {
                NSString *fromString=[toBeString substringToIndex:z];
                NSStringEncoding enc2=CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
                NSData *da2FROM=[fromString dataUsingEncoding:enc2];
                NSInteger fromDataLength2=[da2FROM length];
                if (fromDataLength2==20) {
//                    textView.text=fromString;
                    textView.text=[NSString stringWithFormat:@"%@",toBeString];
                }
                if (fromDataLength2>20) {
//                    [self presentMessageTips:@"最多只能输入10位"];
                    [self presentMessageTips:NSLocalizedString(@"No more than 10 characters", nil)];
                    return;
                }
            }
        }
        else{
            textView.text=[NSString stringWithFormat:@"%@",toBeString];
        }
    }
    _NewTextField=textView;
}
-(IBAction)textFiledReturnEditing:(id)sender{
    [sender resignFirstResponder];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [_NewTextField resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
